import { isObject } from 'lodash-es'
/**
 * 获取存储，判断过期时间，过期删除返回undefined
 * @param key 获取的字段
 * @returns 获取的值
 */
export const getStore = (key: string) => {
  const data: IStorageData = uni.getStorageSync(key)
  // 判断data是否为对象
  if (!isObject(data)) {
    return data
  }
  // 判断是否过期
  if (data.overtime < new Date().getTime()) {
    uni.removeStorageSync(key)
    return void 0
  }
  return data.value
}
/**
 * 设置存储，带过期时间
 * @param key 获取的字段
 * @param value 存储的值
 * @param overtime 过期时间
 */
export const setStore = (key: string, value: unknown, overtime = Number.MAX_SAFE_INTEGER - 1) => {
  // 组合存储数据
  const data: IStorageData = {
    value,
    overtime
  }
  uni.setStorageSync(key, data)
}

export const deleteStore = (key: string) => {
  uni.removeStorageSync(key)
}
